var form;
var table;
var layer;
var arr=[];
layui.use(['layer','form','table'],function(){
    form = layui.form;
    table=layui.table;
    layer=layui.layer;
    selCoupon();


    table.on('checkbox(test)', function(obj){

        var checkStatus=table.checkStatus("table_id");
        var data=checkStatus.data;

        //获取用户id
        var userId=parent.$("#user_id").val();
        for(var a=0;a<data.length;a++){
            var couponId=data[a].coupon_id;
            json={"couponId":couponId,"userId":userId,"state":3};
            arr.push(json);
        }





    });
});

//优惠券数据
function selCoupon(){

    //优惠券名称
    var couponName=$("input[name=couponName]").val();
    $.ajax({
        url: "/SyOperationController/selCoupon",
        type: "post",
        dataType: "json",
        data:{"couponName":couponName},
        success: function (data) {
            getTable(data.obj);
        }
    });
}

//优惠券数据列表
function getTable(data){
    var tb1=table.render({
        elem:"#table_id",
        title:"优惠券数据列表",
        limit:5,
        data:data,
        width:756,
        page:true,
        cols:[[
            {type:'checkbox',width:60},
            {field:'coupon_name',title:'优惠券名称',width:200,align:'center'},
            {field:'coupon_price',title:'面额',width:120,templet:function(d){
                    if(d.coupon_price){
                        return "￥ "+d.coupon_price;
                    }else{
                        return "";
                    }

                }},
            {field:'type',title:'使用条件',width:150,align:'center',templet:function(d){
                    if(d.type==2){
                        return "商品"
                    }else if(d.type=3){
                        return "视频"
                    }else if(d.type==4){
                        return "无限制"
                    }else{
                        return "异常";
                    }
                }},
            {field:'',title:'有效期',width:220,align:'center',templet:function(d){
                    var start=d.start_date;
                    start = start.slice(0, start.indexOf("."));
                    start = start.replaceAll('T',' ');

                    var end=d.end_date;
                    end =end.slice(0, end.indexOf("."));
                    end= end.replaceAll('T',' ');
                    return "<p>开始："+start+"</p>"+"<p>结束："+end+"</p>"
                }},
        ]]
    });
}
//确定
function addCouponUsed(){

    if(arr.length>0){
        $.ajax({
            url: "/SyOperationController/addCouponUsed",
            type: "post",
            dataType: "json",
            data:{"str":JSON.stringify(arr)},
            success: function (data) {
                if(data.code==0){
                    layer.msg('赠送成功');
                }else{
                    layer.msg('赠送失败');
                }
            }
        });
    }else{
        layer.msg("请先选择优惠券");
    }

}
//取消
function cancel() {
    var layer_1=parent.layer_1;
    layer_1.close(layer_1.index);
}